#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

FILE *open_file(const char *fpath, const char *mode)
{
    FILE *f = fopen(fpath, mode);
    if(!f)
    {
        perror(NULL);
        fprintf(stderr, "ERROR: could not open file: ");
        exit(EXIT_FAILURE);
    }
    return f;
}

int main(void)
{
    errno = 0;
    FILE *f = open_file("text.txt", "r");
    char *buffer = NULL;
    size_t bufsize = 0;
    while(1){
        int n_read = getline(&buffer, &bufsize, f);
        if(n_read < 0) break;
        printf("%s", buffer);
    }
    if(errno)
    {
        perror(NULL);
        fprintf(stderr, "ERROR: at %s: %s, line %d\n", __FILE__, __FUNCTION__, __LINE__);
        if(buffer)
        {
            free(buffer);
            exit(EXIT_FAILURE);
        }
    }
    puts("Leyendo de stdin\n> ");
    getline(&buffer, &bufsize, stdin);
    printf("%s", buffer);
    fclose(f);
    free(buffer);                           //DO NOT FORGET
    return 0;
}
